'''
DynaSPCFORC
Plots resultants from SPCFORC file. Takes as input SPCFORC file, and plots the
resultants vs time.
'''
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
import sys

def SPCFORCPLOT(inputfile):
    SPCFORC = Path(inputfile)
    #print(inputfile.parent)
    
    # create null lists for grabbing data from spcforc file
    times = []
    forces = []
    
    #open file
    with SPCFORC.open() as f:
        for line in f:
            #if file is output header, grab the time and append to times list
            if(line[1:7] == 'output'):
                times.append(line[17:-1])
            #if file is force resultant line, grab the forces and append to forces list
            if(line[13:18] == 'force'):
                forces.append(line[34:-1])
    
    # create a np array from the times list
    timecol = np.array(times).astype(np.float)
    
    #create an empty force column array
    fcol = np.empty([len(forces),3])
    # and loop through and replace values with np arrays from the force list
    for i in range(0,len(forces)):
        fcol[i] = np.fromstring(forces[i], dtype=float, sep=' ')
    
    #now create a dataframe with the columns labeled properly
    df_final = pd.DataFrame(fcol, index=timecol,columns=['Rx','Ry','Rz'])
    
    #and plot the Y-reaction forces in kN.
    loadplot = df_final.Ry.divide(1000).plot()
    loadplot.set(title='Load vs Time',xlabel='Time (s)', ylabel='Load (kN)')
    plt.show()
    Ry_max = df_final.Ry.divide(4448.2).max()
    Ry_min = df_final.Ry.divide(4448.2).min()
    print('Peak loads are {:.3f}, {:.3f} kip.'.format(Ry_max,Ry_min))

def main( *argv ):
    '''Main function
    
    '''
    inputargs = argv[0]
    SPCFORCPLOT(inputargs[0])

if __name__ == "__main__":
    main(sys.argv[1:])
